package com.work.washer.controller;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.work.washer.dto.*;
import com.work.washer.model.*;
import com.work.washer.service.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author cys
 * @date 2024/5/7 15:30
 */
@Tag(name = "1")
@RestController
@RequestMapping("/")
public class ShopController {

    @Autowired
    private GoodsClassService goodsClassService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private SkuService skuService;

    @Autowired
    private OrderCommentService orderCommentService;

    @Autowired
    private CartService cartService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderGoodsService orderGoodsService;
    @Autowired
    private UserService userService;

    @Operation(summary = "获取商品分类")
    @GetMapping("/getGoodClass")
    public List<GoodsClass> getGoodClass() {
        return goodsClassService.list();
    }

    @Operation(summary = "获取商品列表 ")
    @GetMapping("/getGoodList")
    public List<Goods> getGoodList(@RequestParam("class_id") Integer classId) {
        return goodsService.lambdaQuery().eq(Goods::getClassId, classId).list();
    }

    @Operation(summary = "1  ")
    @GetMapping("/getGoodDetalis")
    public GoodsVo getGoodDetalis(@RequestParam("id") Integer id) {
        Goods good = goodsService.getById(id);
        List<Sku> list = skuService.lambdaQuery().eq(Sku::getGoodsId, id).list();
        GoodsVo data = new GoodsVo();
        data.setGood(good);
        data.setSku(list);
        return data;
    }

    @Operation(summary = "  2")
    @GetMapping("/getGoodComment")
    public List<OrderComment> getGoodComment(@RequestParam("id") Integer id) {
        List<OrderComment> list = orderCommentService.lambdaQuery().eq(OrderComment::getGoodsId, id).list();
        return list;
    }

    @Operation(summary = " 3 ")
    @GetMapping("/addCart")
    public List<CartVo> getCart(@RequestParam("user_id") Integer userId) {
        List<CartVo> vos = new ArrayList<>();
        List<Cart> list = cartService.lambdaQuery().eq(Cart::getUserId, userId).list();
        list.forEach(item -> {
            CartVo cartVo = new CartVo();
            cartVo.setCart(item);
            cartVo.setSku(skuService.getById(item.getSkuId()));
            cartVo.setGoodsInfo(goodsService.getById(item.getGoodsId()));
            vos.add(cartVo);
        });
        return vos;
    }

    @Operation(summary = " 4 ")
    @PostMapping("/addCart")
    public Boolean addCart(@RequestBody Cart cart) {
        cartService.save(cart);
        return true;
    }

    @Operation(summary = " 5 ")
    @PostMapping("/addOrder")
    public Boolean addOrder(@RequestBody OrderReq req) {
        Order order = req;
        order.setOrderNo("D" + System.currentTimeMillis());
        order.setCreateTime(new Date());
        order.setStatus(2);
        orderService.save(order);
        req.getGoods().forEach(e -> {
            e.setOrderId(order.getId());
        });
        orderGoodsService.saveBatch(req.getGoods());
        req.getGoods().forEach(e -> {
            cartService.lambdaUpdate().eq(Cart::getUserId, req.getUserId()).eq(Cart::getGoodsId, e.getGoodsId()).remove();
        });
        return true;
    }

    @Operation(summary = " 6 ")
    @GetMapping("/getOrder")
    public List<Order> getOrder(@RequestParam("user_id") Integer user_id, @RequestParam(value = "status", required = false) Integer status) {
        if (status == null) {
            status = 0;
        }
        LambdaQueryChainWrapper<Order> query = orderService.lambdaQuery().eq(Order::getUserId, user_id);
        if (status != 0) {
            query = query.eq(Order::getStatus, status);
        }
        List<Order> list = query.list();
        list.forEach(e -> {
            List<OrderGoods> orderGood = orderGoodsService.lambdaQuery().eq(OrderGoods::getOrderId, e.getId()).list();
            orderGood.forEach(j -> {
                j.setGoodsInfo(goodsService.getById(j.getGoodsId()));
            });
            e.setGoods(orderGood);
        });
        return list;
    }

    @Operation(summary = " 7 ")
    @PostMapping("/cancalOrder")
    public Boolean cancalOrder(@RequestBody CancelOrderReq req) {
        orderService.lambdaUpdate().eq(Order::getId, req.getOrder_id()).set(Order::getStatus, 0).update();
        return true;
    }

    @Operation(summary = "8  ")
    @PostMapping("/orderSend")
    public Boolean orderSend(@RequestBody CancelOrderReq req) {
        orderService.lambdaUpdate().eq(Order::getId, req.getOrder_id()).set(Order::getStatus, 3).update();
        return true;
    }

    @Operation(summary = " 9 ")
    @PostMapping("/finishOrder")
    public Boolean finishOrder(@RequestBody CancelOrderReq req) {
        orderService.lambdaUpdate().eq(Order::getId, req.getOrder_id()).set(Order::getStatus, 4).update();
        return true;
    }

    @Operation(summary = " 10 ")
    @GetMapping("/getOrderDetails")
    public Order getOrderDetails(@RequestParam("order_id") Integer orderId) {
        Order order = orderService.getById(orderId);
        List<OrderGoods> list = orderGoodsService.lambdaQuery().eq(OrderGoods::getOrderId, orderId).list();
        list.forEach(e -> {
            e.setGoodsInfo(goodsService.getById(e.getGoodsId()));
        });
        order.setOrder_goods(list);
        return order;
    }

    @Operation(summary = " 11 ")
    @GetMapping("/orderComment")
    public List<OrderComment> orderComment(@RequestParam("id") Integer id) {
        List<OrderComment> list = orderCommentService.lambdaQuery().eq(OrderComment::getGoodsId, id).list();
        list.forEach(e -> {
            e.setUser(userService.getById(e.getUserId()));
        });
        return list;
    }

    @Operation(summary = "12  ")
    @PostMapping("/orderComment")
    public Boolean addOrderComment(@RequestBody OrderCommentReq req) {
        String[] split = req.getGood_id().split(",");
        for (int i = 0; i < split.length; i++) {
            OrderComment comment = new OrderComment();
            comment.setOrderId(req.getOrder_id());
            comment.setContent(req.getContent());
            comment.setCreateTime(new Date());
            comment.setGoodsId(req.getGood_id());
            comment.setUserId(req.getUser_id());
            orderCommentService.save(comment);
        }
        return true;
    }

    @Operation(summary = "13  ")
    @GetMapping("/searchGoods")
    public List<Goods> searchGoods(@RequestParam("name") String name) {
        List<Goods> list = goodsService.lambdaQuery().like(Goods::getTitle, name).list();
        return list;
    }

}
